package ledrpt

import org.kdx.DateUtils

import java.text.SimpleDateFormat

class DwsCycleTimeController {
    def dynamicQueryService
    def cycleList() {
        params.SYS_QUERY_NAME='cycleList'
        def cycleList =dynamicQueryService.queryForList(params)
        [cycleList:cycleList]
    }
    //��ȡ����Cycle��Ϣ
    def updateDwsCycle(DwsCycleTime c){
        render (view:'updateDwsCycle',
                model:[cycleTime:c])
    }
    //�������޸ĵ���������Ϣ
    def addCycle(DwsCycleTime cfu){
        cfu.save()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsCycleTime.label', default: 'DwsCycleTime'), cfu.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }
    //ɾ������������Ϣ
    def deleteCycle(DwsCycleTime c){

        c.delete()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsCycleTime.label', default: 'DwsCycleTime'), c.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""

    }
    //������ʾ
    def dwsCycleTime() {

        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()-30))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        params.SYS_QUERY_NAME = 'cyclePartNameList'
        def cyclePartNameList = dynamicQueryService.queryForList(params)

        def dayList = []
        for (def i=0;i<cyclePartNameList.size();i++){
            dayList.push(cyclePartNameList[i].cycle_prat_name)
        }
        print dayList
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]

        params.SYS_QUERY_NAME = 'dwsCycleList'
        def dataListNo = dynamicQueryService.queryForList(params)
        def totalCount = dataListNo.size()
        params.SYS_QUERY_NAME = 'yieldChartdwsCycle'
        def dataList = dynamicQueryService.queryForList(params)
        println startTime
        def legendDataList = ['QTY','TIME',]
        def percentItemList = ['TIME']
        def seriesData = []
        for(def k=0;k<legendDataList.size();k++){
            def seryHourData = []
            for(def j=0;j<dayList.size();j++){
                def flag = false;
                for(def i=0;i<dataList.size();i++){
                    if(dayList[j]==dataList[i].V_Date){
                        seryHourData.push(dataList[i][legendDataList[k]])
                        flag = true;
                    }
                }
                if(!flag){
                    seryHourData.push(0)
                }
            }
            seriesData.push(seryHourData)
        }
        println seriesData
        def testDataList = []

        [cyclePartNameList:cyclePartNameList,
         startTime:startTime,endTime:endTime,
         cyclePartName:params.cyclePartName?:'',scrapWoId:params.scrapWoId?:'',
         percentItemList:percentItemList,dayList:dayList,legendDataList:legendDataList,seriesData:seriesData,testDataList:testDataList,
         pageSize:pageSize,totalCount:totalCount,pageSizeList:pageSizeList,dataListNo:dataListNo,pageCurrent:pageCurrent]
    }
    def cycleDetail(){
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()-30))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)

        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startNo = (pageSize * (pageCurrent - 1)) + 1
        params.endNo = pageSize * pageCurrent
        params.SYS_QUERY_NAME = 'cyclePartNameList'
        def cyclePartNameList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'cycleDetailList'
        def cycleDetailList = dynamicQueryService.queryForList(params)
        def pageSizeList = [10, 20, 50, 100]
        params.SYS_QUERY_NAME = 'cycleDetailCount'
        def cycleDetailCount = dynamicQueryService.queryForList(params)
        def totalCount =cycleDetailCount.TOTALCOUNT[0]
        [startTime:startTime,endTime:endTime,cycleDetailList:cycleDetailList,cyclePartNameList:cyclePartNameList,cyclePartName:params.cyclePartName,
         pageSize:pageSize,totalCount:totalCount,pageSizeList:pageSizeList,pageCurrent:pageCurrent]
    }



    def exportExcel() {
        def sysQueryName

        sysQueryName = params.SYS_QUERY_NAME

        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        def stepWip

        if (params.wipTime == null || params.wipTime == "") {
            params.SYS_QUERY_NAME = 'stepWip'
            stepWip = dynamicQueryService.queryForList(params)
        } else {

            params.SYS_QUERY_NAME = 'stepWip7h'
            stepWip = dynamicQueryService.queryForList(params)
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,stepWip:stepWip])
    }


}
